import { OneModal } from '@/components/one-ui';
import { Button } from 'antd';
import React, { Ref, useRef } from 'react';
import { IConfirmModal, positionAPI } from '../../type';

import MyIcon from '@/components/MyIcon';

export const ConfirmModal = React.forwardRef(
  (
    {
      onClose,
      visible,
      onReload, //刷新管理页面
      chanageDeptState,
      tableRowObj,
      leftRowCheck,
    }: positionAPI.IConfirmModal,
    ref,
  ) => {
    const confirmRef: Ref<IConfirmModal> = useRef(null); //弹框的ref

    // 关闭modal
    const onCloseModal = () => {
      if (confirmRef) {
        if (confirmRef.current) {
          if (confirmRef.current.onModalCancel) {
            confirmRef.current.onModalCancel();
          }
        }
      }
    };

    // 处理提示信息
    const getTipStr = () => {
      let tip =
        '禁用后，该岗位及其下属岗位和人员均会级联关闭和禁用，是否确认禁用？';
      if (tableRowObj?.pIsOpen == 0) {
        tip = '对不起，该岗位上级岗位已禁用，请先启动上级岗位！';
      } else {
        if (leftRowCheck?.isOpen == 0) {
          tip = '对不起，该岗位所在部门已禁用，请先启动部门！';
        }
      }
      return tip;
    };

    // 点击按钮调用接口
    const clickOnOk = () => {
      if (tableRowObj?.pIsOpen == 0) {
        onCloseModal();
      } else {
        if (leftRowCheck?.isOpen == 0) {
          onCloseModal();
        } else {
          chanageDeptState(false, tableRowObj);
          onCloseModal();
        }
      }
    };

    return (
      <>
        <OneModal
          ref={confirmRef}
          open={true}
          getContainer={false}
          title={
            <div style={{ display: 'flex', alignItems: 'center' }}>
              <span
                style={{ marginRight: 10, fontSize: '24px', color: '#faad14' }}
              >
                <MyIcon type="icon-info-circle" />
              </span>
              <span>禁用岗位</span>
            </div>
          }
          width={document.body.clientWidth * 0.2}
          onCancel={onClose}
          footer={
            <>
              <div>
                <Button onClick={onCloseModal}>取消</Button>
                <Button type={'primary'} onClick={clickOnOk}>
                  确定
                </Button>
              </div>
            </>
          }
          bodyStyle={{
            padding: '15px 30px',
          }}
        >
          <div style={{ fontSize: '18px', color: '#ff000d' }}>
            {getTipStr()}
          </div>
        </OneModal>
      </>
    );
  },
);
